var utils = require('./../../utils/util')
import * as echarts from './../../components/ec-canvas/echarts';

function initChart (canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // 像素
  });
  canvas.setChart(chart);

  var option = {
    xAxis: {
      axisTick: {
        show: false //是否显示坐标刻度
      },
      axisLine: {
        lineStyle: {
          width: 2,
          color: '#666'
        }
      },
      type: 'category',
      boundaryGap: false,
      data: ['1', '2', '3', '4', '5', '6', '7']
    },
    yAxis: {
      show: false,
      type: 'value'
    },
    grid: {
      left: 20,
      bottom: 20,
      right: 20,
      top: 20
    },
    series: [{
      data: [0, 100, 0, 300, 400, 0, 900],
      type: "line",
      hoverAnimation: true,
      itemStyle: {
        borderColor: "#06ae56",
        color: '#06ae56'
      },
      lineStyle: {
        width: 1,
        type: "solid"
      }
    }],
    tooltip: {
      triggerOn: "click",
      alwaysShowContent: true
    }
  }
  chart.setOption(option);
  return chart;
}
Page({
  data: {
    ec: {
      onInit: initChart
    },
    currentActive: 'week', // tabbar默认选择‘周’
    weekList: [], // 每周的数据数组
    progressList: [
      {
        'icon': 'iconjiaotong',
        'percentage': 10,
        'expend': 100,
        'text': '交通'
      },
      {
        'icon': 'iconzhaopianphoto177',
        'percentage': 90,
        'expend': 900,
        'text': '数码'
      }
    ]
  },
  onLoad () {
    let currentDate = utils.formatTime(new Date())
    let arr = this.makeWeekList('2020-01-01 00:00:00', currentDate)
    this.setData({
      weekList: arr
    })
  },
  // 生成weekList
  makeWeekList (startDate, endDate) {
    // 获取当前年份
    let currentYear = (new Date(startDate)).getFullYear()
    // 生成毫秒数
    let startDateDetail = new Date(startDate).getTime()
    let endDateDetail = new Date(endDate).getTime()
    // 计算差
    let difference = endDateDetail - startDateDetail
    // 计算总的周次
    let weekCount = Math.floor(difference / 24 / 3600 / 1000 / 7)
    // 创建数组
    let arr = []
    // 为数组赋值
    for (let i = 0; i < weekCount; i++) {
      arr[i] = `${currentYear}-${i + 1}周`
    }
    return arr
  },

  // 点击week tabbar
  handleWeek (e) {
    this.setData({
      currentActive: e.currentTarget.dataset.date
    })
  },
  // 点击month tabbar
  handleMonth (e) {
    this.setData({
      currentActive: e.currentTarget.dataset.date
    })
  },
  // 点击year tabbar
  handleYear (e) {
    this.setData({
      currentActive: e.currentTarget.dataset.date
    })
  }
})